﻿using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace Quintsys.Core.Services
{
    public interface IEmailMessageFactory
    {
        /// <summary>
        ///     Gets the e-mail message that can be sent using System.Net.Mail.Smtp class.
        /// </summary>
        /// <param name="emailName">Name of the email.</param>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        /// <exception cref="System.Configuration.ConfigurationErrorsException"></exception>
        MailMessage GetMailMessage(string emailName, dynamic model);
    }

    public class EmailMessageFactory : IEmailMessageFactory
    {
        private readonly IEmailTemplateResolver _emailTemplateResolver;

        public EmailMessageFactory(IEmailTemplateResolver emailTemplateResolver)
        {
            _emailTemplateResolver = emailTemplateResolver;
        }

        /// <summary>
        ///     Gets the e-mail message that can be sent using System.Net.Mail.Smtp class.
        /// </summary>
        /// <param name="emailName">Name of the email.</param>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        /// <exception cref="System.Configuration.ConfigurationErrorsException"></exception>
        public MailMessage GetMailMessage(string emailName, dynamic model)
        {
            string subject = GetSubjectFromConfigurationSettings(emailName);
            string templatePhysicalPath = GetTemplatePhysicalPathFromConfigurationSettings(emailName);
            dynamic body = _emailTemplateResolver.GetEmailBody(templatePhysicalPath, model);

            var message = new MailMessage
                {
                    BodyEncoding = Encoding.UTF8,
                    IsBodyHtml = true,
                    Subject = subject,
                    Body = body
                };
            return message;
        }

        /// <summary>
        ///     Gets the template physical path from configuration settings.
        /// </summary>
        /// <param name="emailName">Name of the email.</param>
        /// <returns></returns>
        /// <exception cref="System.Configuration.ConfigurationErrorsException"></exception>
        private static string GetTemplatePhysicalPathFromConfigurationSettings(string emailName)
        {
            var templatePath = ConfigurationManager.AppSettings[string.Format("{0}.TemplatePath", emailName)];
            if (templatePath == null)
                throw new ConfigurationErrorsException(string.Format("Invalid value for {0}.TemplatePath.", emailName));

            var templatePhysicalPath = HttpContext.Current.Server.MapPath(templatePath);
            return templatePhysicalPath;
        }

        /// <summary>
        ///     Gets the subject from configuration settings.
        /// </summary>
        /// <param name="emailName">Name of the email.</param>
        /// <returns></returns>
        /// <exception cref="System.Configuration.ConfigurationErrorsException"></exception>
        private static string GetSubjectFromConfigurationSettings(string emailName)
        {
            var subject = ConfigurationManager.AppSettings[string.Format("{0}.Subject", emailName)];
            if (subject == null)
                throw new ConfigurationErrorsException(string.Format("Invalid value for {0}.Subject.", emailName));
            return subject;
        }
    }
}